﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using LynxWiki.DomainModel;
using LynxWiki.Filters;
using LynxWiki.ViewModels;
using LynxWiki.Search;
using LynxWiki.Repository;

namespace LynxWiki.Controllers
{
    [InitializeSimpleMembership]
    public class ListTopicController : Controller
    {
        //
        // GET: /ListTopic/

        public ActionResult Index(string selectType, string criteria)
        {
            ViewBag.TagCloud = new TagCloud(LuceneSearch.GetTagCount());
            ViewBag.Application = "Version: " + Assembly.GetExecutingAssembly().GetName().Version;
            List<TopicVersion> tvl = null;
            List<WikiTopicListingViewData> lvd = new List<WikiTopicListingViewData>();
            switch (selectType) {
                case "Property":
                    tvl = LuceneSearch.GetTopicsWithProperty(criteria);
                    break;
                case "Tag":
                    tvl = LuceneSearch.GetTopicsWithTag(criteria);
                    break;
                default:
                    break;
            }
            if (tvl != null && tvl.Count > 0) {
                foreach (TopicVersion tv in tvl) {
                    WikiTopicListingViewData tvd = new WikiTopicListingViewData();
                    WikiDirectoryRepository wdr = new WikiDirectoryRepository(Server.MapPath(@"~\" + tv.WikiDir));
                    tvd.WikiDir = tv.WikiDir.Replace(@"\", "/");
                    tvd.TopicName = tv.TopicName;
                    tvd.CreatedOn = tv.CreatedOn;
                    tvd.TopicLocked = wdr.TopicIsLocked(tv.TopicName + "__current.xml");
                    lvd.Add(tvd);
                }
            }
            return View(lvd);
        }

    }
}
